package com.gdufe.firesafe.controller;

import com.gdufe.firesafe.annotation.AuthAccess;
import com.gdufe.firesafe.utils.R;
import com.gdufe.firesafe.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/user/file")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        String url=fileService.upload(file);
        return R.ok().put("data",url);
    }

    @AuthAccess
    @GetMapping("/download/{fileName:.+}")
    public R download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        fileService.download(fileName,response);
        return R.ok();
    }
}
